package com.anti.generate.modular.controller;

import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.annotation.Permission;
import com.anti.commom.api.Result;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.generate.modular.param.CodeGenerateParam;
import com.anti.generate.modular.service.CodeGenerateService;
import com.anti.jdbc.support.page.PageParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 代码生成器
 */
@RestController
public class CodeGenerateController {

    @Resource
    private CodeGenerateService codeGenerateService;

    /**
     * 代码生成基础数据
     */
    @Permission
    @GetMapping("/codeGenerate/page")
    @BusinessLog(title = "代码生成配置_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result page(PageParam pageParam, CodeGenerateParam codeGenerateParam) {
        return Result.data(codeGenerateService.page(pageParam,codeGenerateParam));
    }

    /**
     * 代码生成基础配置保存
     */
    @Permission
    @PostMapping("/codeGenerate/add")
    @BusinessLog(title = "代码生成配置_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(CodeGenerateParam.add.class) CodeGenerateParam codeGenerateParam) {
        this.codeGenerateService.add(codeGenerateParam);
        return Result.success("保存成功");
    }

    /**
     * 代码生成基础配置编辑
     */
    @Permission
    @PostMapping("/codeGenerate/edit")
    @BusinessLog(title = "代码生成配置_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(CodeGenerateParam.add.class) CodeGenerateParam codeGenerateParam) {
        codeGenerateService.edit(codeGenerateParam);
        return Result.success("修改成功");
    }

    /**
     * 删除代码生成基础配置
     */
    @Permission
    @PostMapping("/codeGenerate/delete")
    @BusinessLog(title = "代码生成配置_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(CodeGenerateParam.delete.class) List<CodeGenerateParam> codeGenerateParamList) {
        codeGenerateService.delete(codeGenerateParamList);
        return Result.success("删除成功");
    }

    /**
     * 查询当前数据库用户下的所有表
     */
    @Permission
    @GetMapping("/codeGenerate/InformationList")
    @BusinessLog(title = "数据库表列表_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result InformationList() {
        return Result.data(codeGenerateService.InformationTableList());
    }

    /**
     * 代码生成基础配置生成
     */
    @Permission
    @PostMapping("/codeGenerate/runLocal")
    @BusinessLog(title = "代码生成_本地项目", opType = LogAnnotionOpTypeEnum.OTHER)
    public Result runLocal(@RequestBody @Validated(CodeGenerateParam.detail.class) CodeGenerateParam codeGenerateParam) {
        this.codeGenerateService.runLocal(codeGenerateParam);
        return Result.success("");
    }

    /**
     * 代码生成基础配置生成
     */
    @Permission
    @GetMapping("/codeGenerate/runDown")
    @BusinessLog(title = "代码生成_下载方式", opType = LogAnnotionOpTypeEnum.OTHER)
    public void runDown(@Validated(CodeGenerateParam.detail.class) CodeGenerateParam codeGenerateParam, HttpServletResponse response) {
        this.codeGenerateService.runDown(codeGenerateParam, response);
    }
}
